﻿{include file="public/header"/}
</head>
<body>
<style>
@media screen and (max-width:500px){
    .layui-layer.layui-layer-page{width:100% !important;overflow:hidden !important;left:0 !important;}
    .layui-layer-title{width:100% !important;box-sizing:border-box;}
    .layui-layer-content{width:100% !important;}
}
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">账号：</label>
                        <div class="layui-input-inline mr0">
                            <input name="user_name" class="layui-input" type="text" placeholder="输入账号"/>
                        </div>
                    </div>                    
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">手机：</label>
                        <div class="layui-input-inline mr0">
                            <input name="mobile" class="layui-input" type="text" placeholder="输入手机"/>
                        </div>
                    </div>
                    <div class="layui-inline" style="padding-right:110px;">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchUser" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnAddUser" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>
            <table class="layui-table" id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="tableBarUser">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="tableStateUser">
<input type="checkbox" lay-filter="ckStateUser" data-id="{{d.admin_id}}" value="{{d.admin_id}}" lay-skin="switch" lay-text="正常|锁定" {{d.status==1 ? 'checked':''}}/>
</script>
{include file="public/footer"/}
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            url: "{:url('/system.admin/index')}",
            page: true,
            cellMinWidth: 100,
            cols: [[
                {field:'admin_id',title:'ID',width:60},
                {field: 'user_name', title: '账号'},
                {field: 'pen_name', title: '昵称'},
                {field: 'mobile', title: '手机'},
                {field: 'email',  title: '邮箱'},
                {field: 'sex', title: '性别',width: 80,align: 'center'},
                {field: 'head_pic', title: '头像',templet: function (d) {
                    if (d.head_pic) {
                        return '<a href="javascript:void(0);" onclick=viewImage("' + d.head_pic + '")><image style="max-width:26px;max-height:26px;" src="' + d.head_pic + '"/></a>';
                    } else {
                        return '<a href="javascript:void(0);" onclick=viewImage("{__ADMIN_PATH}/default.png")><image style="max-width:26px;max-height:26px;" src="{__ADMIN_PATH}/default.png"/></a>';
                    }
                }, width: 80},
                {field: 'role_id', templet: function (d) {
                        var str = '<span class="layui-badge-rim">' + d.rolename + '</span>';
                        return str;
                    }, title: '角色'
                },
                {field: 'add_time', sort: true, templet: function (d) {
                    return util.toDateString(d.add_time);
                }, title: '创建时间'},
                {field: 'status', templet: '#tableStateUser', title: '状态',width: 100},
                {align: 'center', toolbar: '#tableBarUser', title: '操作', minWidth: 200}
            ]]
        });
        // 添加
        $('#btnAddUser').click(function () {
            showEditModel();
        });
        // 搜索
        form.on('submit(formSubSearchUser)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });
        // 工具条点击事件
        table.on('tool(tableUser)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(data.admin_id, data.user_name);
            } else if (layEvent === 'reset') { // 重置密码
                resetPsw(data.admin_id, data.user_name);
            }
        });
        // 显示表单弹窗
        function showEditModel(mUser) {
            if(mUser){
                var id = mUser.admin_id;
                layer.open({
                    type: 2,
                    title: '修改管理员',
                    fixed: true,
                    shadeClose: false,
                    shade: 0.3,
                    area: ['100%', '100%'],
                    content: '{:url("/system.admin/edit")}?id='+id
                });
            }
            else{
                layer.open({
                    type: 2,
                    title: '添加管理员',
                    fixed: true,
                    shadeClose: false,
                    shade: 0.3,
                    area: ['100%', '100%'],
                    content: '{:url("/system.admin/add")}'
                });
            }
        };
        //删除
        function doDel(admin_id, nickName) {
            layer.msg('确定要删除“' + nickName + '”吗？', {
                btnAlign: 'c',
                time: 0,
                btn: ['确定', '取消'],
                yes: function(index, layero){
                    layer.close(index);
                    $.get("{:url('/system.admin/del')}", {
                        admin_id: admin_id
                    }, function (res) {
                        layer.closeAll('loading');
                        if (res.status) {
                            layer.msg(res.msg, {icon: 1,time:1000});
                            insTb.reload({}, 'data');
                        } else {
                            layer.msg(res.msg, {icon: 2,time:1000});
                        }
                    }, 'json');
                }
            });
        };
        //修改用户状态
        form.on('switch(ckStateUser)', function (obj) {
            $.get("{:url('/system.admin/updateState')}", {
                admin_id: $(obj.elem).attr('data-id'),
                state: obj.elem.checked ? 0 : 1
            }, function (res) {
                layer.closeAll('loading');
                if (res.status) {
                    layer.msg(res.msg, {icon: 1,time:1000});
                } else {
                    layer.msg(res.msg, {icon: 2,time:1000});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });
        //重置密码
        function resetPsw(admin_id, nickName) {
            layer.confirm('确定要重置“' + nickName + '”的登录密码吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                $.get("{:url('/system.admin/restPsw')}", {
                    admin_id: admin_id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.status) {
                        layer.msg(res.msg, {icon: 1,time:1000});
                    } else {
                        layer.msg(res.msg, {icon: 2,time:1000});
                    }
                }, 'json');
            });
        }
    });
</script>
</body>
</html>